<?php

namespace app\index\controller\hotel;

use app\admin\model\hotel\HotelOrder;
use app\admin\model\hotel\HotelRoom;
use app\common\controller\Hotel;

/**
 * 主页
 * @internal
 */
class Console extends Hotel
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    
    public function _initialize()
    {
        parent::_initialize();
    }
    
    public function index()
    {
        $hotel_id = $this->shop->hotel_id;
        $seventtime = \fast\Date::unixtime('day', -6);

        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $time = $seventtime + ($i * 86400);
            $day = date("Y-m-d", $time);
            $start = $time;
            $end = $time + 86400;
            // 订单数
            $createlist[$day] = HotelOrder::where('createtime','between', [$start,$end])
                ->where(['hotel_id' => $hotel_id])
                ->count();
            // 成交数
            $paylist[$day] = HotelOrder::where('createtime','between', [$start,$end])
                ->where('order_status', 10)
                ->where(['hotel_id' => $hotel_id])
                ->count();
        }

        //七日增长
        $week = HotelOrder::whereTime('createtime', 'week')->where('hotel_id', $hotel_id)->count();
        $lastweek = HotelOrder::whereTime('createtime', 'last week')->where('hotel_id', $hotel_id)->count();
        if($week != 0 && $lastweek != 0 ){
            $sevendnu = bcdiv(bcmul(100, bcsub($week, $lastweek, 2), 2), $lastweek, 2);
        }else{
            $sevendnu = 0;
        }
        $this->view->assign([
            'totalroom'        => HotelRoom::where('hotel_id', $hotel_id)->count(), //商品总数
            'totalorder'       => HotelOrder::where('hotel_id', $hotel_id)->count(), // 总订单数
            'totalorderamount' => HotelOrder::where('hotel_id', $hotel_id)->sum('pay_money'), //总金额
            'todayorder'       => HotelOrder::whereTime('createtime', 'today')->where('hotel_id', $hotel_id)->count(), // 今日订单数量
            'sevendnu'         => $sevendnu.'%',
            'sevendau'         => HotelOrder::whereTime('createtime', 'today')->where('hotel_id', $hotel_id)->sum('pay_money'), //今日销售额
            'paylist'          => $paylist,
            'createlist'       => $createlist
        ]);
        return $this->view->fetch();
    }
}


